package com.dome.popup.myapplication;

import android.animation.IntEvaluator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.PopupWindowCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.dome.popup.myapplication.animation.ExpandableSelectorAnimator;
import com.dome.popup.myapplication.expandable.ExpandablePopup;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private TextView tv;
    private TextView tv1;
    private TextView tv2;
    private TextView tv4;

    @SuppressLint("HandlerLeak")
    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            ExpandablePopup popup = new ExpandablePopup(MainActivity.this);
            switch (msg.what){
                case 1:
                    initializeIconsExpandableSelector(popup);
                    popup.animOrientation(ExpandableSelectorAnimator.ANIM_TYPE_LEFT);
                    PopupWindow expandablePopupWin = new PopupWindow(MainActivity.this);
                    expandablePopupWin.setContentView(popup);
                    expandablePopupWin.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                    expandablePopupWin.setHeight(CommonUtil.dip2px(MainActivity.this,60));
                    expandablePopupWin.setOutsideTouchable(false);
                    expandablePopupWin.setFocusable(true);
                    expandablePopupWin.setAnimationStyle(R.style.pop_animation);
                    expandablePopupWin.showAsDropDown(tv,0,  - (tv.getHeight()+expandablePopupWin.getHeight()));
                    break;
                case 2:
                    FrameLayout.LayoutParams ly = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                    ly.gravity = Gravity.RIGHT;
                    popup.animOrientation(ExpandableSelectorAnimator.ANIM_TYPE_RIGHT);
                    popup.setLayoutParams(ly);
                    initializeIconsExpandableSelector(popup);
//                    addContentView(popup,ly);
                    PopupWindow expandablePopupWinRight = new PopupWindow(MainActivity.this);
                    expandablePopupWinRight.setContentView(popup);
                    expandablePopupWinRight.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                    expandablePopupWinRight.setHeight(CommonUtil.dip2px(MainActivity.this,60));
                    expandablePopupWinRight.setOutsideTouchable(false);
                    expandablePopupWinRight.setFocusable(true);
                    expandablePopupWinRight.setAnimationStyle(R.style.pop_animation_left);
                    PopupWindowCompat.showAsDropDown(expandablePopupWinRight, tv1, 0, 0, Gravity.END);
                    break;
                    default:
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.tv);
        tv1 = findViewById(R.id.tv1);
        tv2 = findViewById(R.id.tv2);
        tv4 = findViewById(R.id.tv4);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myHandler.sendEmptyMessage(1);
            }
        });
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myHandler.sendEmptyMessage(2);
            }
        });
        tv4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                performAnimate(tv4,tv4.getWidth(),tv4.getWidth()*2);
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                performAnimate(tv2,tv2.getWidth(),300);
            }
        });
    }

    private void performAnimate(final View target, final int start, final int end) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            //持有一个IntEvaluator对象，方便下面估值的时候使用
            private IntEvaluator mEvaluator = new IntEvaluator();

            @Override
            public void onAnimationUpdate(ValueAnimator animator) {
                //获得当前动画的进度值，整型，1-100之间
                int currentValue = (Integer)animator.getAnimatedValue();

                //计算当前进度占整个动画过程的比例，浮点型，0-1之间
                float fraction = currentValue / 100f;

                //直接调用整型估值器通过比例计算出宽度，然后再设给Button
                target.getLayoutParams().width = mEvaluator.evaluate(fraction, start, end);
                target.requestLayout();
            }
        });

        valueAnimator.setDuration(3500).start();}

    private void initializeIconsExpandableSelector(final ExpandablePopup popup) {
        List<ExpandableItem> expandableItems = new ArrayList<ExpandableItem>();
        ExpandableItem item = new ExpandableItem();
        item.setResourceId(R.drawable.api_ag);
        expandableItems.add(item);
        ExpandableItem item1 = new ExpandableItem();
        item1.setResourceId(R.drawable.api_comingsoon);
        expandableItems.add(item1);
        ExpandableItem item2 = new ExpandableItem();
        item2.setResourceId(R.drawable.api_ebet);
        expandableItems.add(item2);
        ExpandableItem item3 = new ExpandableItem();
        item3.setResourceId(R.drawable.api_ebet);
        expandableItems.add(item3);
        popup.showExpandableItems(expandableItems);
        popup.setOnExpandableItemClickListener(new OnExpandableItemClickListener() {
            @Override public void onExpandableItemClickListener(int index, View view) {
                switch (index) {
                    case 0:
                        showToast("00000000");
                        break;
                    case 1:
                        showToast("1111111111");
                        break;
                    case 2:
                        showToast("2222222222222");
                        break;
                    default:
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}
